<?php


namespace App\Model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\URL;

/**
 * App\Model\Goods
 *
 * @property int $id
 * @property int $category_id 分类id
 * @property string $goods_name 商品名
 * @property int $goods_thumb_id 商品缩略图id
 * @property string $goods_thumb 商品缩略图路径
 * @property string $thumb_url 商品缩略图路径
 * @property float $goods_price 商品价格
 * @property int $sale_num 销量
 * @property string|null $detail 商品描述
 * @property string|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereDetail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereGoodsName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereGoodsPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereGoodsThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereGoodsThumbId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereSaleNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int|null $status 商品状态
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereStatus($value)
 * @property-read \App\Model\Category $category
 * @property string|null $out_url 外链url
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Goods whereOutUrl($value)
 */
class Goods extends Model
{

    const STATUS_ONLINE = 1;
    const STATUS_OFFLINE = 0;

    public function getGoodsThumbUrl()
    {
        return $this->thumb_url ?: (URL::asset('/uploads/' . $this->goods_thumb) ?: '');
    }

    public static function findGoods()
    {
        return Goods::orderBy('sale_num', 'desc');
    }

    /**
     * 获取上架商品
     * @param $cat_id
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * @author thanatos thanatos915@163.com
     */
    public static function getGoodsByCat($cat_id)
    {
        return Goods::where('status', static::STATUS_ONLINE)
            ->where('category_id', $cat_id)
            ->orderBy('sale_num', 'desc')
            ->get();
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }


}
